﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Web.Mvp;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Common;
using DotNetNuke.Services.Exceptions;

namespace YourCompany.Modules.$safeprojectname$
{
    public class Edit$safeprojectname$Presenter : ModulePresenter<IEdit$safeprojectname$, Edit$safeprojectname$Model>
    {
        #region "Private Members"
        private I$safeprojectname$Controller controller;
        #endregion

        #region "Constructors"

        /// <summary>
        /// Initializes a new instance of the <see cref="Edit$safeprojectname$Presenter"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public Edit$safeprojectname$Presenter(IEdit$safeprojectname$ view)
            : this(view, new $safeprojectname$Controller(DataProvider.Instance()))
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="Edit$safeprojectname$Presenter"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="controller">The controller.</param>
        public Edit$safeprojectname$Presenter(IEdit$safeprojectname$ view, I$safeprojectname$Controller controller)
            : base(view)
        {
            if (controller == null)
                throw new ArgumentNullException("controller", "controller is null.");
            this.controller = controller;
            View.Cancel += new EventHandler(View_Cancel);
            View.Delete += new EventHandler(View_Delete);
            View.Save += new EventHandler<UIEventArgs<$safeprojectname$Info>>(View_Save);
        }

        #endregion

        #region "Properties"
        /// <summary>
        /// Gets the item id.
        /// </summary>
        public int ItemId
        {
            get
            {
                var itemId = Null.NullInteger;
                if (!string.IsNullOrEmpty(Request.QueryString["ItemId"]))
                {
                    itemId = Int32.Parse(Request.QueryString["ItemId"]);
                }
                return itemId;
            }
        }
        #endregion

        #region "Event Handler Implementation"
        /// <summary>
        /// Called when [init].
        /// </summary>
        protected override void OnInit()
        {
            base.OnInit();
            if (!IsPostBack)
            {
                try
                {
                    if (DotNetNuke.Common.Utilities.Null.NullInteger != ItemId)
                    {
                        // get content
                        var obj$safeprojectname$Info = controller.Get$safeprojectname$(ModuleId, ItemId);
                        if (obj$safeprojectname$Info != null)
                        {
                            View.Model.Item = obj$safeprojectname$Info;
                        }
                        else
                        { //' security violation attempt to access item not related to this Module
                            Response.Redirect(Globals.NavigateURL(TabId));
                        }
                    }
                    else
                    {
                        View.Model.Item = new $safeprojectname$Info(ModuleId, UserId);
                        View.ShowDeleteButton(false);
                        View.ShowAuditControl(false);
                    }
                }
                catch (Exception exc)
                {
                    //'Show a UI message later
                    Exceptions.LogException(exc);
                }
            }
        }

        /// <summary>
        /// Handles the Save event of the View control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void View_Save(object sender, UIEventArgs<$safeprojectname$Info> e)
        {
            try
            {
                e.Item.CreatedByUser = UserId;
                e.Item.ModuleId = ModuleId;
                e.Item.ItemId = ItemId;
                if (DotNetNuke.Common.Utilities.Null.NullInteger == ItemId)
                {
                    //add the content within the $safeprojectname$ table
                    e.Item.ItemId = controller.Add$safeprojectname$(e.Item);
                    if (e.Item.ItemId == Null.NullInteger)
                    {
                        throw new Exception("Add Exception");
                    }
                    else
                    {
                        // update the content within the $safeprojectname$ table
                        controller.Update$safeprojectname$(e.Item);
                    }
                }
                //' Redirect back to the portal home page
                Response.Redirect(Globals.NavigateURL(TabId));
            }
            catch (Exception exc)
            {//    'Module failed to load
                //'Show a UI message later
                Exceptions.LogException(exc);
            }
        }

        /// <summary>
        /// Handles the Delete event of the View control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void View_Delete(object sender, EventArgs e)
        {
            try
            {
                // Only attempt to delete the item if it exists already
                var obj$safeprojectname$ = controller.Get$safeprojectname$(ModuleId, ItemId);
                if (obj$safeprojectname$ != null)
                {
                    controller.Delete$safeprojectname$(ModuleId, ItemId);
                    // Redirect back to the portal home page
                    Response.Redirect(Globals.NavigateURL(TabId));
                }
                else
                { // security violation attempt to access item not related to this Module
                    Response.Redirect(Globals.NavigateURL(TabId));
                }
            }
            catch (Exception exc)
            {
                //Show a UI message later
                Exceptions.LogException(exc);
            }
        }

        /// <summary>
        /// Handles the Cancel event of the View control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void View_Cancel(object sender, EventArgs e)
        {
            Response.Redirect(Globals.NavigateURL(TabId));
        }
        #endregion
    }
}